//
//  ViewController.m
//  fmdb_demo
//
//  Created by rujia chen on 15/10/16.
//  Copyright (c) 2015年 dfrz. All rights reserved.
//

#import "ViewController.h"

#import "BBSService.h"

#import "TopicModel.h"
#import "TopicCell.h"

#import "TopicDetailViewController.h"

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>{
    NSMutableArray *_dataArray;
}

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
}

- (void)loadData {
    [BBSService listEssentialTopWithSuccessBlock:^(ResponseModel *model) {
        _dataArray = [[NSMutableArray alloc]init];
        [_dataArray addObjectsFromArray:[TopicModel listWithArray:model.result[@"list"]]];
        [self.tableView reloadData];
    } failBlock:^(ResponseModel *model) {
        
    } networkFailBlock:^(NSError *error) {
        
    }];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TopicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"news_cell"];
    [cell setModel:_dataArray[indexPath.row]];
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 66;
}

#pragma mark - segues

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.identifier isEqualToString:@"topic_detail_segue"]) {
        TopicDetailViewController *vc = segue.destinationViewController;
        vc.topicModel = ((TopicCell*)sender).model;
    }
}

@end
